package cloud.shyidong.tenant.controller;

import cloud.shyidong.core.context.TenantContext;
import cloud.shyidong.core.context.TenantContextHolder;
import cloud.shyidong.core.controller.AbstractController;
import cloud.shyidong.core.request.LongIdRequest;
import cloud.shyidong.tenant.dao.TenantProjectDao;
import cloud.shyidong.tenant.entity.TenantProjectEntity;
import cloud.shyidong.tenant.request.TenantProjectInsertRequest;
import cloud.shyidong.tenant.request.TenantProjectPageRequest;
import cloud.shyidong.tenant.request.TenantProjectSaveRequest;
import cloud.shyidong.tenant.request.TenantProjectUpdateRequest;
import cloud.shyidong.tenant.service.impl.ProjectService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/9/14
 */
@RestController
@RequestMapping("/tenant-project")
@Slf4j
public class ProjectController extends AbstractController<ProjectService, TenantProjectDao, TenantProjectEntity, LongIdRequest, TenantProjectInsertRequest, TenantProjectUpdateRequest, TenantProjectPageRequest> {
    
    @GetMapping("/get")
    public TenantProjectEntity get() {
        TenantContext tenantContext = TenantContextHolder.getTenantContext();
        return getService().getByTenantId(tenantContext.getTenantId());
    }
    
    @PostMapping("/save")
    public TenantProjectEntity save(@RequestBody @Valid TenantProjectSaveRequest tenantProjectSaveRequest) {
        if (log.isDebugEnabled()) {
            log.debug("save param >>> {}", JSON.toJSONString(tenantProjectSaveRequest));
        }
        TenantContext tenantContext = TenantContextHolder.getTenantContext();
        tenantProjectSaveRequest.setTenantId(tenantContext.getTenantId());
        return getService().save(tenantProjectSaveRequest);
    }
    
}
